using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for FriendManager
/// </summary>
public class FriendManager
{
    #region gets
    public static DataTable GetFriends()
    {
        //List<Friend> lisReturnCollection = null;
        DataTable dtaReturnTable = null;

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetFriends", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        //Friend friTemFriend;
                        //lisReturnCollection = new List<Friend>();
                        dtaReturnTable = new DataTable();

                        DataRow droTemp;

                        for (int intCounter = 0; intCounter < reader.FieldCount; intCounter++)
                        {
                            dtaReturnTable.Columns.Add(reader.GetName(intCounter));
                        }

                        do
                        {

                            droTemp = dtaReturnTable.NewRow();

                            for (int intCounter = 0; intCounter < reader.FieldCount; intCounter++)
                            {
                                droTemp[reader.GetName(intCounter)] = reader[intCounter];
                            }

                            dtaReturnTable.Rows.Add(droTemp);

                            //friTemFriend = new Friend();

                            //friTemFriend.Alt = reader["FRI_ALT"].ToString();
                            //friTemFriend.Description = reader["FRI_DESCRIPTION"].ToString();
                            //friTemFriend.Display = reader["FRI_DISPLAY"].ToString();
                            //friTemFriend.SortOrder = Convert.ToInt32(reader["FRI_SORT"]);
                            //friTemFriend.URL = reader["FRI_URL"].ToString();
                            //friTemFriend.ID = Convert.ToInt32(reader["FRI_ID"]);
                            //friTemFriend.Name = reader["FRI_NAME"].ToString();

                            //lisReturnCollection.Add(friTemFriend);

                        } while (reader.Read()) ;// end of the read while loop

                    }// end of the if check to see if anything was found
                }// end reader using              
            }// end command using
        }// end connection using

       // return lisReturnCollection;
        return dtaReturnTable;
    }// end of the GetFriends method

    public static Friend GetFriend(int ID)
    {
        Friend friTemFriend = new Friend();

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetFriend", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("FRI_ID", ID));

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        friTemFriend.Alt = reader["FRI_ALT"].ToString();
                        friTemFriend.Description = reader["FRI_DESCRIPTION"].ToString();
                        friTemFriend.Display = reader["FRI_DISPLAY"].ToString();
                        friTemFriend.SortOrder = Convert.ToInt32(((reader["FRI_SORT"] == DBNull.Value) ? 0 : reader["FRI_SORT"]));
                        friTemFriend.URL = reader["FRI_URL"].ToString();
                        friTemFriend.ID = Convert.ToInt32(reader["FRI_ID"]);
                        friTemFriend.Name = reader["FRI_NAME"].ToString();

                    }// end of the read while loop
                }// end reader using              
            }// end command using
        }// end connection using

        return friTemFriend;
    }// end of the GetFriend method
    #endregion

    #region updates
    public static bool UpdateFriend(Friend friend)
    {
        int intRowsUpdated = 0;
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("UpdateFriend", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@ID", friend.ID));
                command.Parameters.Add(new SqlParameter("@Desc", friend.Description));
                command.Parameters.Add(new SqlParameter("@Name", friend.Name));
                command.Parameters.Add(new SqlParameter("@Alt", friend.Alt));
                command.Parameters.Add(new SqlParameter("@Url", friend.URL));
                command.Parameters.Add(new SqlParameter("@Display", friend.Display));

                connection.Open();
                intRowsUpdated = command.ExecuteNonQuery();

            }// end command using
        }// end connection using

        return ((intRowsUpdated == 0) ? false : true);

    }// end of the UpdateFriend method
    #endregion

    #region inserts
    public static bool InsertFriend(Friend friend)
    {
        int intRowsUpdated = 0;
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("AddFriend", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@Desc", friend.Description));
                command.Parameters.Add(new SqlParameter("@Name", friend.Name));
                command.Parameters.Add(new SqlParameter("@Alt", friend.Alt));
                command.Parameters.Add(new SqlParameter("@Url", friend.URL));
                command.Parameters.Add(new SqlParameter("@Display", friend.Display));

                connection.Open();
                intRowsUpdated = command.ExecuteNonQuery();

            }// end command using
        }// end connection using

        return ((intRowsUpdated == 0) ? false : true);

    }// end of the InsertFriend method

    #endregion

    #region deletes

    public static bool DeleteFriend(int FRI_ID)
    {
        int intRowsUpdated = 0;
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("RemoveFriend", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@FRI_ID", FRI_ID));

                connection.Open();
                intRowsUpdated = command.ExecuteNonQuery();

            }// end command using
        }// end connection using

        return ((intRowsUpdated == 0) ? false : true);

    }// end of the DeleteFriend method
    #endregion

}// end of the FriendManager class
